<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\facade\View;
use think\exception\ValidateException;

// 推荐数据概览
class OverView extends BaseController
{


    public function jobFunction()
    {
        return View::fetch();
    }



    public function getJobFunctionList(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }
        $get = $request->get();

        $total = Db::name('job_function')->count();

        $rows = Db::name('job_function')
            ->page($get['page'], $get['limit'])
            ->select();

        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => $total,
            'data'=> $rows
        );
        return json($data);
    }


    public function user(Request $request)
    {
        $type = $request->get('type');
        $id = $request->get('id');

        View::assign('type', $type);
        View::assign('id', $id);
        return View::fetch();
    }

    public function getMembersList(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $get = $request->get();
        $where = [];

        if (isset($get['type'])) {
            if ($get['type'] == 'job_function') {
                $function = Db::name('job_function')->where('id', '=', $get['id'])->value('function');
                $where['a.function'] = $function;
                $table = 'o_job_function_user';
            } else {
                $age_id = Db::name('age')->where('id', '=', $get['id'])->value('age_id');
                $where['a.age_id'] = $age_id;
                $table = 'o_age_user';
            }
        }


        if (isset($get['key'])) {
            if (!empty($get['key'])) {
                $where[] = ['b.name|b.userid', 'like', '%'.$get['key'].'%'];
            }
        }

        $total = Db::table("$table")
            ->alias('a')
            ->join('o_members b', 'b.userid=a.userid')
            ->where($where)
            ->count();


        $rows = Db::table("$table")
            ->alias('a')
            ->join('o_members b', 'b.userid=a.userid')
            ->where($where)
            ->field('b.userid, b.name, b.department_name, b.age')
            ->page($get['page'], $get['limit'])
            ->select()
            ->toArray();

        foreach ($rows as &$v) {
            $count = mb_strlen($v['userid']);
            if ($count == 6) {
                if (is_numeric($v['userid'])) {
                    $v['userid'] = '00'.$v['userid'];
                }
            }
        }

        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => $total,
            'data'=> $rows
        );
        return json($data);
    }


    public function collection(Request $request)
    {
        $type = $request->get('type');
        $id = $request->get('id');
        View::assign('type', $type);
        View::assign('id', $id);

        return View::fetch();
    }


    public function getCourseList(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $get = $request->get();
        $where = [];

        if ($get['type'] == 'job_function') {
            $function = Db::name('job_function')->where(array('id'=>$get['id']))->value('function');
            $where['function'] = $function;
            $table = 'o_job_function_course';
        } else {
            $age_id = Db::name('age')->where(array('id'=>$get['id']))->value('age_id');
            $where['age_id'] = $age_id;
            $table = 'o_age_course';
        }


        $total = Db::table("$table")
            ->where($where)
            ->count();

        $rows = Db::table("$table")
            ->where($where)
            ->field('id, courseid, type')
            ->page($get['page'], $get['limit'])
            ->select()
            ->toArray();

        foreach ($rows as &$v) {
            if ($v['type'] == 'spark') {
                $title = Db::name('learning')
                    ->where('id', '=', $v['courseid'])
                    ->value('title');
                $v['seminar'] = $title;
                $v['type_name'] = 'SPARK';

            } elseif ($v['type'] == 'qxt') {
                $course_info = Db::name('qxt_courses')->where('id', '=', $v['courseid'])->field('courseTitle, category')->find();
                $v['seminar'] = $course_info['courseTitle'];
                $v['type_name'] = '轻学堂';
            }
        }

        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => $total,
            'data'=> $rows
        );
        return json($data);
    }

    public function age()
    {
        return View::fetch();
    }

    public function getAgeList(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $get = $request->get();

        $total = Db::name('age')->count();

        $rows = Db::name('age')
            ->page($get['page'], $get['limit'])
            ->select();

        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => $total,
            'data'=> $rows
        );
        return json($data);
    }

}